package com.tinem.platform.module.starter.web.filter;

import com.tinem.platform.module.pojo.co.GatewayHeadName;
import com.tinem.platform.module.starter.web.context.GatewayContext;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * @author fengzhihao
 * @version v1
 * @program: platform
 * @className LogMdcFilter
 * @description TODO
 * @site 
 * @company 
 * @create 2020-07-04 10:06
 */
@Slf4j
@AllArgsConstructor
public class LogFilter implements Filter {
    io.opentracing.Tracer tracer;
    String artifactId;
    String instanceName;
    @Override
    public void init(FilterConfig filterConfig) {

    }
    @Override
    public void destroy() {

    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        final HttpServletRequest request = (HttpServletRequest) servletRequest;
        final HttpServletResponse response = (HttpServletResponse) servletResponse;
        String gateway_request_id = request.getHeader(GatewayHeadName.X_PLATFORM_GATEWAY_REQUEST_ID);
        String opentracing_id = tracer.activeSpan().context().toTraceId();
        String jti = GatewayContext.getJwtJit();
        MDC.put(GatewayHeadName.X_PLATFORM_GATEWAY_REQUEST_ID,gateway_request_id);
        MDC.put(GatewayHeadName.X_PLATFORM_GATEWAY_CONTEXT_JTI,jti);
        log.info("request {}:({}) fastlog gateway_request_id: [{}]; opentracing_id: [{}]; jti: [{}]; uri:[{}]; queryString:[{}]"
                ,artifactId
                ,instanceName
                ,gateway_request_id
                ,opentracing_id
                ,jti
                ,request.getRequestURI()
                ,request.getQueryString()

        );
        if(log.isDebugEnabled()){
            Enumeration headerNames = request.getHeaderNames();
            while (headerNames.hasMoreElements()){
                String name	=(String) headerNames.nextElement();
                String value = request.getHeader(name);
                log.debug("req head {}:{}",name,value);
            }
            Enumeration paramsNames = request.getParameterNames();
            while (paramsNames.hasMoreElements()){
                String name	=(String) paramsNames.nextElement();
                String value = request.getParameter(name);
                log.debug("req params {}:{}",name,value);
            }


        }
        filterChain.doFilter(request,response);
        if(log.isDebugEnabled()){
            response.getHeaderNames().stream().forEach(name->{
                String value = response.getHeader(name);
                log.debug("res head {}:{}",name,value);
            });
        }
        MDC.remove(GatewayHeadName.X_PLATFORM_GATEWAY_CONTEXT_JTI);
        MDC.remove(GatewayHeadName.X_PLATFORM_GATEWAY_REQUEST_ID);
    }
}
